﻿using System;
using EasyShop.UserService.App.Interface;
using EasyShop.UserService.Request;
using EasyShop.UserService.Response;
using Infrastructure;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace EasyShop.UserService.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private readonly IUserApp _userApp;
        public UserController(IUserApp userApp)
        {
            _userApp = userApp;
        }
        [Authorize]
        [HttpGet]
        public Response<UserView> Get(string id)
        {
            var result = new Response<UserView>();
            try
            {
                result.Result = _userApp.Get(id);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }
        [HttpPost]
        public Response<string> Register(UserRegisterReq request)
        {
            var result = new Response<string>();
            try
            {
                result.Result = _userApp.Register(request);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }
    }
}
